/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormasPago.java
 *
 * Created on 19-oct-2013, 16:28:25
 */
package inscripciones.vista;

import inscripciones.DAO.ConsultasBD;
import inscripciones.Utiles.Mensajes;
import inscripciones.Utiles.Util;
import inscripciones.modelo.Abono;
import inscripciones.modelo.Abono.Enumtipo;
import inscripciones.modelo.Categoria;
import inscripciones.modelo.Pago;
import inscripciones.modelo.Participante;
import inscripciones.modeloTablas.ModelTableFormaPago;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author DJ2-PC
 */
public class FormasPago extends javax.swing.JDialog {

    private Pago pago;
    private List<Abono> abonos;
    private Participante participante;
    private List<Categoria> categorias;
    private Abono abono;
    private Abono.Enumtipo[] tipos;
    private Categoria categoria;
    int variable = 0;
    ModelTableFormaPago modele;
    String bandera = "A";

    /** Creates new form FormasPago */
    public FormasPago(java.awt.Frame parent, boolean modal, Participante participante) {
        super(parent, modal);

        this.participante = participante;
        this.categorias = (ArrayList<Categoria>) new ConsultasBD().buscarTodos("Categoria");
        if (participante != null) {
            if (this.participante.getPago() != null) {
                ////his.pago = this.participante.getPago();
                ArrayList<Pago> pagos = (ArrayList<Pago>) new ConsultasBD().buscarPOR("Pago", "id", String.valueOf(this.participante.getPago().getId()));
                if (!pagos.isEmpty()) {
                    this.pago = pagos.get(0);
                }
            } else {
                this.pago = new Pago();
            }

            if (this.pago.getAbonos() != null) {
                ////this.abonos = this.pago.getAbonos();
                ArrayList<Abono> abonosaux = (ArrayList<Abono>) new ConsultasBD().buscarPOR("Abono", "forma", String.valueOf(this.pago.getId()));
                if (!abonosaux.isEmpty()) {
                    this.abonos = abonosaux;
                } else {
                    this.abonos = new ArrayList<Abono>();
                }
            } else {
                this.abonos = new ArrayList<Abono>();
            }
            this.categoria = this.participante.getCategoria();
        } else {
            this.participante= new Participante();
            this.pago = new Pago();
            this.abonos = new ArrayList<Abono>();
            this.categoria = categorias.get(0);
        }

        this.abono = new Abono();
        this.tipos = Abono.Enumtipo.values();
        initComponents();
        jcbTipo.setModel(new DefaultComboBoxModel(tipos));
        if (tipos.length > 0) {

            selected();
        }

        if (pago != null) {
            actualizartabla();
            calcularFaltante();
        }
        abonosEliminados= new ArrayList<Abono>();
        jcbCategoria.setSelectedIndex(categorias.indexOf(this.categoria));
        jcbCategoria.requestFocus();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        buttonGroup1 = new javax.swing.ButtonGroup();
        imagePanel1 = new com.mycsistemas.swingec.panel.ImagePanel();
        curvesPanel1 = new com.mycsistemas.swingec.panel.CurvesPanel();
        jLabel2 = new javax.swing.JLabel();
        jrbSIVA = new javax.swing.JRadioButton();
        jrbIVA = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        imagePanel2 = new com.mycsistemas.swingec.panel.ImagePanel();
        jtxtSuman = new javax.swing.JTextField();
        jlblFaltan = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jtxtFaltan = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtblPagos = new javax.swing.JTable();
        jtxtTotalCancelar = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jcbCategoria = new javax.swing.JComboBox();
        imagePanel4 = new com.mycsistemas.swingec.panel.ImagePanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jtxtnro = new javax.swing.JTextField();
        jcbTipo = new javax.swing.JComboBox();
        jtxtValor = new javax.swing.JTextField();
        jbtnAgregar = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jbtnNuevo = new javax.swing.JButton();
        JBTNcALC = new javax.swing.JButton();

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Formas de Pago");

        imagePanel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/inscripciones/Recursos/''_10102012_1738.jpeg"))); // NOI18N

        curvesPanel1.setLayout(null);

        jLabel2.setFont(new java.awt.Font("Elephant", 0, 14));
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Categoría :");
        curvesPanel1.add(jLabel2);
        jLabel2.setBounds(50, 60, 76, 30);

        buttonGroup1.add(jrbSIVA);
        jrbSIVA.setFont(new java.awt.Font("Elephant", 0, 14));
        jrbSIVA.setForeground(new java.awt.Color(255, 255, 255));
        jrbSIVA.setText("Sin Iva");
        jrbSIVA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbSIVAActionPerformed(evt);
            }
        });
        curvesPanel1.add(jrbSIVA);
        jrbSIVA.setBounds(360, 100, 90, 27);

        buttonGroup1.add(jrbIVA);
        jrbIVA.setFont(new java.awt.Font("Elephant", 0, 14));
        jrbIVA.setForeground(new java.awt.Color(255, 255, 255));
        jrbIVA.setSelected(true);
        jrbIVA.setText("12% Iva");
        jrbIVA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbIVAActionPerformed(evt);
            }
        });
        curvesPanel1.add(jrbIVA);
        jrbIVA.setBounds(260, 100, 87, 27);

        jLabel1.setFont(new java.awt.Font("Georgia", 3, 18));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Formas de Pago");
        curvesPanel1.add(jLabel1);
        jLabel1.setBounds(160, 30, 152, 21);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/inscripciones/Recursos/left_32.png"))); // NOI18N
        jButton2.setText("REGRESAR");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jButton2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jButton2KeyPressed(evt);
            }
        });
        curvesPanel1.add(jButton2);
        jButton2.setBounds(140, 530, 220, 40);

        imagePanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Tabla de Pagos", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, java.awt.Color.white));
        imagePanel2.setForeground(new java.awt.Color(255, 255, 255));

        jtxtSuman.setBackground(new java.awt.Color(255, 255, 51));
        jtxtSuman.setFont(new java.awt.Font("Tahoma", 1, 24));

        jlblFaltan.setFont(new java.awt.Font("Elephant", 0, 14));
        jlblFaltan.setForeground(new java.awt.Color(255, 255, 255));
        jlblFaltan.setText("Faltan $ :");

        jLabel8.setFont(new java.awt.Font("Elephant", 0, 14));
        jLabel8.setForeground(new java.awt.Color(255, 255, 255));
        jLabel8.setText("Suman $ :");

        jtxtFaltan.setBackground(new java.awt.Color(0, 0, 0));
        jtxtFaltan.setEditable(false);
        jtxtFaltan.setFont(new java.awt.Font("Tahoma", 1, 12));
        jtxtFaltan.setForeground(new java.awt.Color(255, 0, 0));

        jtblPagos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Tipo", "Valor"
            }
        ));
        jtblPagos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtblPagosMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtblPagos);

        javax.swing.GroupLayout imagePanel2Layout = new javax.swing.GroupLayout(imagePanel2);
        imagePanel2.setLayout(imagePanel2Layout);
        imagePanel2Layout.setHorizontalGroup(
            imagePanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(imagePanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(imagePanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(imagePanel2Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, imagePanel2Layout.createSequentialGroup()
                        .addGroup(imagePanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(imagePanel2Layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtxtSuman, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(imagePanel2Layout.createSequentialGroup()
                                .addGap(57, 57, 57)
                                .addComponent(jlblFaltan)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jtxtFaltan, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(29, 29, 29))))
        );
        imagePanel2Layout.setVerticalGroup(
            imagePanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(imagePanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(imagePanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtxtSuman, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(imagePanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtFaltan, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblFaltan, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        curvesPanel1.add(imagePanel2);
        imagePanel2.setBounds(40, 140, 410, 210);

        jtxtTotalCancelar.setBackground(new java.awt.Color(0, 0, 0));
        jtxtTotalCancelar.setEditable(false);
        jtxtTotalCancelar.setFont(new java.awt.Font("Tahoma", 1, 14));
        jtxtTotalCancelar.setForeground(new java.awt.Color(255, 51, 204));
        curvesPanel1.add(jtxtTotalCancelar);
        jtxtTotalCancelar.setBounds(140, 100, 110, 30);

        jLabel7.setFont(new java.awt.Font("Elephant", 0, 14));
        jLabel7.setForeground(new java.awt.Color(255, 255, 255));
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel7.setText("Valor $ :");
        curvesPanel1.add(jLabel7);
        jLabel7.setBounds(60, 100, 70, 30);

        jcbCategoria.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${categorias}");
        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, jcbCategoria);
        bindingGroup.addBinding(jComboBoxBinding);

        curvesPanel1.add(jcbCategoria);
        jcbCategoria.setBounds(140, 60, 210, 30);

        imagePanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Abono", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(255, 255, 255)));

        jLabel5.setFont(new java.awt.Font("Elephant", 0, 14));
        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setText("Valor:");

        jLabel4.setFont(new java.awt.Font("Elephant", 0, 14));
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("Tipo: ");

        jLabel6.setFont(new java.awt.Font("Elephant", 0, 14));
        jLabel6.setForeground(new java.awt.Color(255, 255, 255));
        jLabel6.setText("Nro:");

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${abono.nro}"), jtxtnro, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jtxtnro.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtxtnroKeyPressed(evt);
            }
        });

        jcbTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jcbTipo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jcbTipoKeyPressed(evt);
            }
        });

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${abono.valor}"), jtxtValor, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jtxtValor.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtxtValorKeyPressed(evt);
            }
        });

        jbtnAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/inscripciones/Recursos/tick_32.png"))); // NOI18N
        jbtnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAgregarActionPerformed(evt);
            }
        });
        jbtnAgregar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbtnAgregarKeyPressed(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/inscripciones/Recursos/trash_32.png"))); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jButton3.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jButton3KeyPressed(evt);
            }
        });

        jbtnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/inscripciones/Recursos/document_32.png"))); // NOI18N
        jbtnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnNuevoActionPerformed(evt);
            }
        });
        jbtnNuevo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbtnNuevoKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout imagePanel4Layout = new javax.swing.GroupLayout(imagePanel4);
        imagePanel4.setLayout(imagePanel4Layout);
        imagePanel4Layout.setHorizontalGroup(
            imagePanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(imagePanel4Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(imagePanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 60, Short.MAX_VALUE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(imagePanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jtxtnro)
                    .addGroup(imagePanel4Layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addGroup(imagePanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jcbTipo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jtxtValor, javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE))))
                .addGap(18, 18, 18)
                .addGroup(imagePanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jButton3, 0, 0, Short.MAX_VALUE)
                    .addComponent(jbtnAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 38, Short.MAX_VALUE))
                .addGap(20, 20, 20))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, imagePanel4Layout.createSequentialGroup()
                .addContainerGap(339, Short.MAX_VALUE)
                .addComponent(jbtnNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(22, 22, 22))
        );
        imagePanel4Layout.setVerticalGroup(
            imagePanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, imagePanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(imagePanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(imagePanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jcbTipo, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jbtnNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(5, 5, 5)
                .addGroup(imagePanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(imagePanel4Layout.createSequentialGroup()
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(imagePanel4Layout.createSequentialGroup()
                        .addGroup(imagePanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtxtValor, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jbtnAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(5, 5, 5)
                        .addGroup(imagePanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 34, Short.MAX_VALUE)
                            .addComponent(jtxtnro, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        curvesPanel1.add(imagePanel4);
        imagePanel4.setBounds(40, 360, 410, 160);

        JBTNcALC.setIcon(new javax.swing.ImageIcon(getClass().getResource("/inscripciones/Recursos/icono_calculadora1.gif"))); // NOI18N
        JBTNcALC.setText("CALC.");
        JBTNcALC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JBTNcALCActionPerformed(evt);
            }
        });
        curvesPanel1.add(JBTNcALC);
        JBTNcALC.setBounds(360, 60, 90, 30);

        javax.swing.GroupLayout imagePanel1Layout = new javax.swing.GroupLayout(imagePanel1);
        imagePanel1.setLayout(imagePanel1Layout);
        imagePanel1Layout.setHorizontalGroup(
            imagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(curvesPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 496, Short.MAX_VALUE)
        );
        imagePanel1Layout.setVerticalGroup(
            imagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(imagePanel1Layout.createSequentialGroup()
                .addComponent(curvesPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 591, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(imagePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(imagePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jrbSIVAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbSIVAActionPerformed
        // TODO add your handling code here:

        if (this.isVisible()) {
            calcular();
            if (pago != null) {
                pago.setIva(obtenerIVA());
            }
            calcularFaltante();
        }
    }//GEN-LAST:event_jrbSIVAActionPerformed

    private void jrbIVAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbIVAActionPerformed
        // TODO add your handling code here:
        if (this.isVisible()) {
            calcular();
            if (pago != null) {
                pago.setIva(obtenerIVA());
            }
            calcularFaltante();
        }
    }//GEN-LAST:event_jrbIVAActionPerformed

    private void jbtnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAgregarActionPerformed
        // TODO add your handling code here:
//        if (pago == null) {
//            pago = new Pago();
        pago.setPorcentajeIva(obtenerIVA());
        if (!jtxtValor.getText().isEmpty()) {
            abono.setEnumtipes(tipos[jcbTipo.getSelectedIndex()]);
            if (bandera.equals("A")) {
                //esto estaba
//                this.abono.setForma(pago);
//                this.pago.getAbonos().add(abono);
//                this.abonos = this.pago.getAbonos();
                //hasta aqui
                this.abono.setForma(pago);
                this.abonos.add(this.abono);
                this.pago.setAbonos(abonos);
                this.participante.setPago(this.pago);
            }
            this.abono = new Abono();
            jtxtValor.setText("");
            jtxtnro.setText("");
            bandera = "A";
            jtxtSuman.setText(String.valueOf(sumarTotalAbonado(this.pago)));
//            bindingGroup.unbind();
//            bindingGroup.bind();
            actualizartabla();
            calcularFaltante();
            jcbTipo.requestFocus();
        } else {
            jtxtValor.requestFocus();
        }
    }//GEN-LAST:event_jbtnAgregarActionPerformed

    private void jcbTipoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jcbTipoKeyPressed
        // TODO add your handling code here:

        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            jtxtValor.requestFocus();
            return;
        }
        cerrarventana(evt);
    }//GEN-LAST:event_jcbTipoKeyPressed

    public void cerrarventana(java.awt.event.KeyEvent evt) {
        if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
            this.dispose();
            return;
        }
    }

    private void jtxtValorKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtxtValorKeyPressed
        // TODO add your handling code here:

        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            if (jcbTipo.getSelectedItem().toString().equals("COMPROBANTE")) {
                jtxtnro.requestFocus();
            } else {
                jbtnAgregar.requestFocus();
                jbtnAgregarActionPerformed(null);
            }
        }
        cerrarventana(evt);
    }//GEN-LAST:event_jtxtValorKeyPressed

    private void jtxtnroKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtxtnroKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            jbtnAgregar.requestFocus();
            jbtnAgregarActionPerformed(null);
        }
        cerrarventana(evt);
    }//GEN-LAST:event_jtxtnroKeyPressed

    private void jtblPagosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtblPagosMouseClicked
        // TODO add your handling code here:
        if (jtblPagos.getSelectedRow() != -1) {
            this.abono = abonos.get(jtblPagos.getSelectedRow());

            if (this.abono.getEnumtipes().compareTo(Enumtipo.EFECTIVO) == 0) {
                jcbTipo.setSelectedItem(Enumtipo.EFECTIVO);
            } else {
                jcbTipo.setSelectedItem(Enumtipo.COMPROBANTE);
            }
            jtxtValor.setText(String.valueOf(this.abono.getValor()));
            jtxtnro.setText(this.abono.getNro());
            bandera = "E";
        }
    }//GEN-LAST:event_jtblPagosMouseClicked

    private void jbtnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnNuevoActionPerformed
        // TODO add your handling code here:
        this.abono = new Abono();
        jtxtValor.setText("");
        jtxtnro.setText("");

    }//GEN-LAST:event_jbtnNuevoActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        if (abono != null) {
            if (new Mensajes().ConfirmacionDialogowarning(this, "¿Desea eliminar este registro?") == 0) {
                //this.participante.getPago().getAbonos().get(jtblPagos.getSelectedRow()).setForma(null);
                eliminarDatos();
//                this.participante.getPago().getAbonos().remove(abono);
                actualizartabla();
                calcularFaltante();
                jbtnNuevoActionPerformed(null);
            }
        }

    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jbtnNuevoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbtnNuevoKeyPressed
        // TODO add your handling code here:
        cerrarventana(evt);
    }//GEN-LAST:event_jbtnNuevoKeyPressed

    private void jbtnAgregarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbtnAgregarKeyPressed
        // TODO add your handling code here:
        cerrarventana(evt);
    }//GEN-LAST:event_jbtnAgregarKeyPressed

    private void jButton3KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jButton3KeyPressed
        cerrarventana(evt);
// TODO add your handling code here:
    }//GEN-LAST:event_jButton3KeyPressed

    private void jButton2KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jButton2KeyPressed
        // TODO add your handling code here:
        cerrarventana(evt);
    }//GEN-LAST:event_jButton2KeyPressed

    private void JBTNcALCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JBTNcALCActionPerformed
        // TODO add your handling code here:
        if (jcbCategoria.getSelectedIndex() != -1) {
            int indice = jcbCategoria.getSelectedIndex();
            this.categoria = this.categorias.get(jcbCategoria.getSelectedIndex());
            
            System.out.println(this.participante+ "-------------");
            System.out.println(this.categoria+ "-------------");
            this.participante.setCategoria(this.categoria);
            selected();
            calcularFaltante();
            jcbTipo.requestFocus();
        }

    }//GEN-LAST:event_JBTNcALCActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FormasPago.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FormasPago.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FormasPago.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FormasPago.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                FormasPago dialog = new FormasPago(new javax.swing.JFrame(), true, null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    public Enumtipo[] getTipos() {
        return tipos;
    }

    public void setTipos(Enumtipo[] tipos) {
        this.tipos = tipos;
    }

    public List<Abono> getAbonos() {
        return abonos;
    }

    public void setAbonos(List<Abono> abonos) {
        this.abonos = abonos;
    }

    public List<Categoria> getCategorias() {
        return categorias;
    }

    public void setCategorias(List<Categoria> categorias) {
        this.categorias = categorias;
    }

    public Pago getPago() {
        return pago;
    }

    public void setPago(Pago pago) {
        this.pago = pago;
    }

    public Participante getParticipante() {
        return participante;
    }

    public void setParticipante(Participante participante) {
        this.participante = participante;
    }

    public Categoria getCategoria() {
        return categoria;
    }

    public void setCategoria(Categoria categoria) {
        this.categoria = categoria;
    }

    public Abono getAbono() {
        return abono;
    }

    public void setAbono(Abono abono) {
        this.abono = abono;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JBTNcALC;
    private javax.swing.ButtonGroup buttonGroup1;
    private com.mycsistemas.swingec.panel.CurvesPanel curvesPanel1;
    private com.mycsistemas.swingec.panel.ImagePanel imagePanel1;
    private com.mycsistemas.swingec.panel.ImagePanel imagePanel2;
    private com.mycsistemas.swingec.panel.ImagePanel imagePanel4;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable2;
    private javax.swing.JButton jbtnAgregar;
    private javax.swing.JButton jbtnNuevo;
    private javax.swing.JComboBox jcbCategoria;
    private javax.swing.JComboBox jcbTipo;
    private javax.swing.JLabel jlblFaltan;
    private javax.swing.JRadioButton jrbIVA;
    private javax.swing.JRadioButton jrbSIVA;
    private javax.swing.JTable jtblPagos;
    private javax.swing.JTextField jtxtFaltan;
    private javax.swing.JTextField jtxtSuman;
    private javax.swing.JTextField jtxtTotalCancelar;
    private javax.swing.JTextField jtxtValor;
    private javax.swing.JTextField jtxtnro;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    public void calcular() {
        if (jrbIVA.isSelected()) {
            jtxtTotalCancelar.setText(String.valueOf(categoria.getValorIva()));
        } else {
            jtxtTotalCancelar.setText(String.valueOf(categoria.getValorNeto()));
        }
    }

    public void selected() {
        if (this.categoria != null) {
            if (this.pago.getId() != null) {
                if (this.pago.getPorcentajeIva() == 12) {
                    jrbIVA.setSelected(true);
                    jtxtTotalCancelar.setText(String.valueOf(categoria.getValorIva()));
                } else {
                    jrbSIVA.setSelected(true);
                    jtxtTotalCancelar.setText(String.valueOf(categoria.getValorNeto()));
                }
            } else {
                jrbIVA.setSelected(true);
                jtxtTotalCancelar.setText(String.valueOf(categoria.getValorIva()));
                this.pago.setPorcentajeIva(12);
            }

        }
    }

    private Double sumarTotalAbonado(Pago p) {
        Double suma = 0.00;
        for (Abono abonoaux : p.getAbonos()) {
            suma = suma + abonoaux.getValor();
        }
        return suma;
    }

    private void calcularFaltante() {
        double faltante = 0.00;
        double suma = new Util().redondear(sumarTotalAbonado(this.pago), 2);
        double valorTotal = Double.parseDouble(jtxtTotalCancelar.getText());
        faltante = valorTotal - suma;
        faltante = new Util().redondear(faltante, 2);

        if (suma == valorTotal) {
            //if (suma == 0.0) {
            jtxtFaltan.setForeground(new java.awt.Color(255, 255, 255));
            jtxtFaltan.setText(String.valueOf(faltante));
            jtxtSuman.setText(String.valueOf(suma));
            jlblFaltan.setText("Faltan:");

            return;
//            } else {
//                jtxtFaltan.setForeground(new java.awt.Color(255, 255, 255));
//                jtxtFaltan.setText(String.valueOf(faltante));
//                jlblFaltan.setText("Faltan:");
//                return;
//            }
        }

        if (suma > valorTotal) {
            jtxtFaltan.setForeground(new java.awt.Color(0, 255, 0));
            jlblFaltan.setText("Sobran:");
            jtxtSuman.setText(String.valueOf(suma));
            jtxtFaltan.setText(String.valueOf(faltante * -1));
            return;
        }

        if (suma < valorTotal) {
            jtxtFaltan.setForeground(new java.awt.Color(255, 0, 0));
            jlblFaltan.setText("Faltan:");
            jtxtSuman.setText(String.valueOf(suma));
            jtxtFaltan.setText(String.valueOf(faltante));
            return;
        }

    }

    private int obtenerIVA() {
        if (jrbIVA.isSelected()) {
            return 12;
        } else {
            return 0;
        }
    }

    private void actualizartabla() {

        modele = new ModelTableFormaPago(this.abonos);
        jtblPagos.setModel(modele);
//        jtblPagos.setDefaultRenderer(JButton.class, new BotonCellRenderer());
//        jtblPagos.setDefaultEditor(JButton.class, new BotonCellEditor(modele, jTable2, "ABONOS", "NUEVO", this.abonos));
        modele.tamanioColumnas(jtblPagos);
        modele.tamanioFilas(jtblPagos);
        //jtblPagos.updateUI();
    }

    private ArrayList<Abono> abonosEliminados;
    private void eliminarDatos() {      
        this.abonosEliminados.add(this.abono);
        this.abono.setForma(null);
        this.abonos.remove(this.abono);
        this.pago.setAbonos(this.abonos);
        this.participante.setPago(pago);
    }

    public ArrayList<Abono> getAbonosEliminados() {
        return abonosEliminados;
    }

    public void setAbonosEliminados(ArrayList<Abono> abonosEliminados) {
        this.abonosEliminados = abonosEliminados;
    }
    
}
